package com.example.backend.service.mysql.user.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.backend.dto.request.PageUserDto;
import com.example.backend.entitly.mysql.user.Users;
import com.example.backend.mapper.mysql.user.UsersMapper;
import com.example.backend.service.mysql.user.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author 1
 * @description 针对表【users(用户表)】的数据库操作Service实现
 * @createDate 2025-06-04 09:01:52
 */
@Service
@Slf4j
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users>
        implements UsersService {
    private static final int DELETED_FLAG = 1;
    private static final int NOT_DELETED_FLAG = 0;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Boolean removeByUserId(Long userId) {
        return new LambdaUpdateChainWrapper<>(getBaseMapper())
                .eq(Users::getUserId, userId)
                .set(Users::getDeleted, DELETED_FLAG)
                .set(Users::getDeletedAt, LocalDateTime.now())
                .update();
    }

    @Override
    public Boolean cancelRemove(Long userId) {
        return new LambdaUpdateChainWrapper<>(getBaseMapper())
                .eq(Users::getUserId, userId)
                .set(Users::getDeleted, NOT_DELETED_FLAG)
                .update();
    }

    private PageUserDto getPageUserDto(PageUserDto pageUserDto) {
        if (pageUserDto.getPageNum() == null || pageUserDto.getPageNum() < 1) {
            pageUserDto.setPageNum(1);
        }
        if (pageUserDto.getPageSize() == null || pageUserDto.getPageSize() < 1) {
            pageUserDto.setPageSize(10);
        }
        return pageUserDto;
    }

    @Override
    public Page<Users> pageListByUser(PageUserDto pageUserDto) {
        PageUserDto pageUserDto1 = getPageUserDto(pageUserDto);
        Page<Users> userPage = new Page<>(pageUserDto1.getPageNum(), pageUserDto1.getPageSize());
        return new LambdaQueryChainWrapper<>(getBaseMapper())
                .page(userPage);
    }

    @Override
    public Users getByRemoveId(Long userId) {
        return new LambdaQueryChainWrapper<>(getBaseMapper())
                .eq(Users::getUserId, userId)
                .eq(Users::getDeleted, DELETED_FLAG)
                .one();
    }

    @Override
    public Boolean removeByPhysicsId(Long userId) {
        int i = getBaseMapper().physicalDeleteById(userId);
        return i > 0;
    }

    @Override
    public Page<Users> pageListByWholesaler(PageUserDto pageUserDto) {
        PageUserDto pageUserDto1 = getPageUserDto(pageUserDto);
        Page<Users> userPage = new Page<>(pageUserDto1.getPageNum(), pageUserDto1.getPageSize());
        return new LambdaQueryChainWrapper<>(getBaseMapper())
                .eq(Users::getParentId, pageUserDto1.getUserId())
                .page(userPage);
    }

    @Override
    public Boolean removeByWholesId(Long userId) {
        return new LambdaUpdateChainWrapper<>(getBaseMapper())
                .eq(Users::getUserId, userId)
                .set(Users::getParentId, null)
                .update();
    }

    @Override
    public Users saveByUser(String username) {
        Users users = new Users();
        users.setUsername(username);
        save(users);
        return users;
    }

    @Cacheable(value = "user", key = "#principal")
    @Override
    public Users getByUserId(Integer principal) {
        log.debug("数据库查询用户：{}", principal);
        return getById(principal);
    }
}




